// pages/recordSound/recordSound.js
const record = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pauses:"暂停",
    tempRecordSrc:"",
    time:0,
    times:"00",
    timeStop:"",
    temp:""
  },
  pause(){
      if(this.data.pauses == "暂停"){
        this.setData({pauses:"继续"});
        record.pause();
        record.onPause(()=>{
            console.log("录音暂停"); 
            clearInterval(this.data.timeStop);
        })
      }else{
        this.setData({pauses:"暂停"});
        record.resume();
        record.onResume(()=>{
            console.log("录音继续");
            var that = this;
            clearInterval(this.data.timeStop);
            this.data.timeStop =  setInterval(()=>{
                that.timeRecord();
            },1000);
        })
      }
  
  },
  play(){
      console.log("开始");
      record.start();
      record.onStart(()=>{
        console.log("录音开始");
        var that = this;
        clearInterval(this.data.timeStop);
        innerAudioContext.stop();
        this.data.timeStop =  setInterval(()=>{
            that.timeRecord();
        },1000);
    })
  },
  stop(){
    console.log("停止");
    record.stop();
    record.onStop((res)=>{
        console.log("录音停止");
        clearInterval(this.data.timeStop);
        this.data.tempRecordSrc = res.tempFilePath;
        innerAudioContext.src = res.tempFilePath;
        innerAudioContext.play();
        this.setData({times:"00"});
        this.data.time = 0;
      
    })
  },
  timeRecord(){   
    let timex = ++this.data.time;
        if(timex<10){
          timex = "0"+timex;
        }
        this.setData({times: timex});

        
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 console.log(record);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})